package com.SMP.service.class_room;


import com.SMP.dao.EquipmentMapper;
import com.SMP.model.Equipment;
import com.SMP.model.EquipmentExample;
import com.SMP.service.BroadcastService;
import com.SMP.utils.Utils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

@Service
public class EquipmentService {

    @Autowired
    EquipmentMapper equipmentMapper;

    public  Equipment selectEquipmentById(String equipmentId){
    Equipment equipment = equipmentMapper.selectByPrimaryKey(equipmentId);
    return equipment;
    }

    public List<Equipment> selectEquipment(Integer pn){
        EquipmentExample equipmentExample=new EquipmentExample();
        EquipmentExample.Criteria criteria=equipmentExample.createCriteria();
        criteria.andStandby1EqualTo("1");
        PageHelper.startPage(pn, 5);
        List<Equipment> equipments=equipmentMapper.selectByExample(equipmentExample);
        for(Equipment equipment:equipments){
            String IP=equipment.getEquipmentIp();
            if (Utils.isIP(IP)){
                try {
                    InetAddress address = InetAddress.getByName(IP);
                    if (address.isReachable(10)){
                        equipment.setEquipmentState("2");
                        System.out.println("Reach: " + IP+"已连接");
                    }else{
                        equipment.setEquipmentState("1");
                        System.out.println("Reach: " + IP+"未连接");
                    }
                } catch (UnknownHostException e) {
                    System.err.println("Unable to lookup "+IP);
                } catch (IOException e) {
                    System.err.println("Unable to reach "+IP);
                }
            }else{
                System.out.println("Not IP "+IP);
                equipment.setEquipmentState("3");
            }
            equipmentMapper.updateByPrimaryKey(equipment);
        }
        PageHelper.startPage(pn, 5);
        List<Equipment> result=equipmentMapper.selectByExample(equipmentExample);
        return result;
    }

    public Equipment selectEquipmentE(EquipmentExample equipmentExample){
        List<Equipment> equipments=equipmentMapper.selectByExample(equipmentExample);
        Equipment equipment=null;
        if (equipments.size()>0){
            equipment=equipments.get(0);
        }
        return equipment;
    }

    public int updateEquipment(Equipment equipment){
        return  equipmentMapper.updateByPrimaryKey(equipment);
    }

    public int addEquipment(Equipment equipment) {
        String id = Utils.getsUUID();
        equipment.setEquipmentId(id);
        return equipmentMapper.insertSelective(equipment);
    }
    /**
     * @author zlz
    //	 * @param 假删
     * */
    public int deleteEquipment(String equipmentId) {
        Equipment equipment = equipmentMapper.selectByPrimaryKey(equipmentId);
        equipment.setStandby1("2");
        return equipmentMapper.updateByPrimaryKey(equipment);
    }
    /**
     * @author TSY
    //	 * @param 根据名字查询设备
     * */
    public  List<Equipment> selectEquipmentByName(String equipmentName){
        List<Equipment> list = equipmentMapper.selectByName(equipmentName);
        return list;
    }
    /**
     * @author TSY
    //	 * @param 根据状态查询设备
     * */
    public  List<Equipment> selectEquipmentByState(String equipmentState){
        List<Equipment> list = equipmentMapper.selectByState(equipmentState);
        return list;
    }
}
